package com.thinksns.sociax.t4.android.popupwindow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.thinksns.sociax.android.R;

/**
 * Created by zhiyicx on 2017/5/15.
 */

public class PopupWindowShowNotify {
    private static PopupWindow dialog;

    public static class Builder {
        private TextView tv_title, tv_content, tv_ok, tv_cancel;
        private String title, content, ok, cancel;
        private Context context;
        private View.OnClickListener okListener, cancelListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setOk(String ok, View.OnClickListener okListener) {
            this.ok = ok;
            this.okListener = okListener;
            return this;
        }

        public Builder setCancel(String cancel, View.OnClickListener cancelListener) {
            this.cancel = cancel;
            this.cancelListener = cancelListener;
            return this;
        }

        public PopupWindow build() {
            View view = initView(R.layout.dialog_show_notify);
            dialog = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            dialog.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null));
            initProperties();
            dialog.setFocusable(true);
            dialog.setTouchable(true);
            dialog.setOutsideTouchable(true);
            return dialog;
        }

        private View initView(int resId) {
            View view = LayoutInflater.from(context).inflate(resId, null);
            tv_title = (TextView) view.findViewById(R.id.dialog_title);
            tv_content = (TextView) view.findViewById(R.id.dialog_content);
            tv_cancel = (TextView) view.findViewById(R.id.dialog_cancel_btn);
            tv_ok = (TextView) view.findViewById(R.id.dialog_ok_btn);
            return view;
        }

        private void initProperties() {
            if (!TextUtils.isEmpty(title)) {
                tv_title.setText(title);
            } else {
                tv_title.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(content)) {
                tv_content.setText(content);
            }

            if (!TextUtils.isEmpty(ok)) {
                tv_ok.setVisibility(View.VISIBLE);
                tv_ok.setText(ok);
                tv_ok.setOnClickListener(okListener);
            } else {
                tv_ok.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(cancel)) {
                tv_cancel.setVisibility(View.VISIBLE);
                tv_cancel.setText(cancel);
                tv_cancel.setOnClickListener(cancelListener);
            } else {
                tv_cancel.setVisibility(View.GONE);
            }
        }
    }
}
